package com.cyy.dyeing.controller;

import com.cyy.dyeing.common.CurrentUser;
import com.cyy.dyeing.common.DyeingConstants;
import com.cyy.dyeing.common.Result;
import com.cyy.dyeing.common.TokenUtils;
import com.cyy.dyeing.dto.MenuDto;
import com.cyy.dyeing.service.ModuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.xml.datatype.DatatypeConstants;
import java.util.List;

/**
 * @Author: cyy
 * @Date: 2024/7/24
 */
@RestController
@RequestMapping("/home")
public class HomeController {
    @Autowired
    private ModuleService moduleService;

    /**
     * 获取在线用户
     * @param clientToken
     * @return
     */
    @GetMapping("/curr_user")
    public Result currentUser(@RequestHeader(DyeingConstants.HEADER_USER_TOKEN) String clientToken){
        //通过令牌工具类解析用户信息
        CurrentUser currentUser = TokenUtils.getCurrentUser(clientToken);
        return Result.ok(currentUser);
    }

    /**
     * 注销用户
     * @param client
     * @return
     */
    @DeleteMapping("/logout")
    public Result logout(@RequestHeader(DyeingConstants.HEADER_USER_TOKEN)String client){
        TokenUtils.removeToken(client);
        return Result.ok();
    }

    /**
     * 菜单列表
     * @param clientToken
     * @return
     */
    @GetMapping("/menu-list")
    public Result menuList(@RequestHeader(DyeingConstants.HEADER_USER_TOKEN)String clientToken){
        CurrentUser currentUser = TokenUtils.getCurrentUser(clientToken);
        List<MenuDto> list = moduleService.getMenuList(currentUser.getUserId());
        return  Result.ok(list);
    }
}
